package com.hisense.ovcloud.apicontrol.tool;

import cn.hutool.core.codec.Base64;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

@Slf4j
public class AES256CbcUtils {

    public static void main(String[] args) {
        String plainTxt = "zzzzz";
        String key = "aaaabbbbccccddddeeeeffffgggghhhh";
//        String iv = key.substring(0, 16);
//        System.out.println("MW： " + plainTxt);
//        System.out.println("KEY： " + key);
//        System.out.println("KEY-Hex： " + HexUtil.encodeHexStr(key));
//        System.out.println("IV： " + iv);
//        System.out.println("IV-Hex： " + HexUtil.encodeHexStr(iv));
        String encrtptedTxt = encrypt256CBCWithoutIv(key,
                plainTxt);
//        System.out.println("encrtptedTxt: " + Base64.encode(encrtptedTxt, StandardCharsets.UTF_8));
//        String deBase64 = new String(Base64.decode(a.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
//        String sha1ToTxt = "yfjLvJutkNLNS7x0aV6nRLBBM7iRJ-IA6dy6U_J6wGR4t50DtDBLXQsDS2tKqSVtappId=47110563037231&randStr=sdadsadadsadsa9&srcType=0&timeStamp=1713771744000&version=7.0&wifiId=86500110001000100010000010b5bc926678D9519A4B756946F081B7BB5B5E8D1197";
//        String sha1ToTxt2 = "yfjLvJutkNLNS7x0aV6nRLBBM7iRJ-IA6dy6U_J6wGR4t50DtDBLXQsDS2tKqSVtappId=47110563037231&physicalDeviceId=86500110001000100010000010b5bc926678&randStr=sdadsadadsadsa11&srcType=0&timeStamp=1713771744000&version=7.0D9519A4B756946F081B7BB5B5E8D1197";

        System.out.println(encrtptedTxt);
//        String a = "TR4oEByluScc0bkqB4jyIEsn6hPt31mlMWEb+Fhis3y+l32FKkEUh3C/q90+2sqX";
//        System.out.println("plainTxt: " + decrypt256CBCWithoutIv(key, a));
    }

    public static String decrypt256CBC(String cryptText, String key, String iv) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
            byte[] s = Base64.decode(cryptText.getBytes(StandardCharsets.UTF_8));
            return new String(cipher.doFinal(s));
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public static String encrypt256CBC(String plainText, String key, String iv) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
            byte[] original = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
            return Base64.encode(original);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public static String encrypt256CBCWithoutIv(String key, String plainText) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(key.substring(0, 16).getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
            byte[] original = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
            return Base64.encode(original);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }


    public static String decrypt256CBCWithoutIv(String key, String cryptText) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(key.substring(0, 16).getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
            byte[] s = Base64.decode(cryptText.getBytes(StandardCharsets.UTF_8));
            return new String(cipher.doFinal(s));
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public static String sha1(String txt) {
        return DigestUtils.sha1Hex(txt);
    }
}

